<template>
  <el-row type="flex" style="width: 100%">
    <el-col>
      <span>
        <span v-if="treeData.pid === '' || isRoot" :style="{color: varibleColor.blue}" class="el-icon-s-home" />
        {{ treeData.name }}-{{ treeData.code }}
      </span>
    </el-col>
    <el-col :span="6">
      <el-row type="flex">
        <el-col>
          <span>{{ treeData.manager }}</span>
        </el-col>
        <el-col>
          <el-dropdown @command="handleClick">
            <span>
              操作
              <i class="el-icon-arrow-right" />
            </span>
            <template>
              <el-dropdown-menu>
                <el-dropdown-item v-if="treeData.pid === '' || isRoot" command="add">添加部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { delDepartments } from '@/api/departments'
import varibleColor from '@/styles/variables.scss'

export default {
  name: 'TreeTools',
  props: {
    isRoot: {
      type: Boolean,
      default: false
    },
    treeData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      varibleColor
    }
  },
  updated() {
    console.log(this.treeData)
  },
  methods: {
    async handleClick(type) {
      // 新增add
      // 编辑edit
      // 删除del
      if (type === 'add') {
      //  新增功能
      //  弹出一个弹出层
        this.$emit('addDept', this.treeData)
      } else if (type === 'edit') {
        this.$emit('editDept', this.treeData)
      } else if (type === 'del') {
      //  删除
        console.log('删除')
        // 弹出一个确认的弹层
        // 弹出层返回的是一个promise
        // 点击了确定的时候，当前的promise才会成功
        await this.$confirm('是否确认删除')
        //  调用删除的接口
        await delDepartments(this.treeData.id)
        //  进行成功地提示
        this.$message.success('删除成功')
        //  删除成功以后重新获取部门数据
        this.$emit('getDepts')
      }
    }
  }
}
</script>
<style></style>
